package linkedList.双指针;

import linkedList.BaseLinkedListTest;
import po.ListNode;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 链表的中间结点876.java
 * @Description https://leetcode.cn/problems/middle-of-the-linked-list/
 * 给定一个头结点为 head 的非空单链表，返回链表的中间结点。
 *
 * 如果有两个中间结点，则返回第二个中间结点
 * @createTime 2021年03月16日 19:54:00
 */
public class 链表的中间结点876 extends BaseLinkedListTest {
    public static ListNode middleNode(ListNode head) {
        if (head.next == null) {
            return head;
        }
        ListNode fastPoint = head.next, slowPoint = head;
        while (fastPoint != null && fastPoint.next != null) {
            slowPoint = slowPoint.next;
            fastPoint = fastPoint.next.next;
        }
        if (fastPoint!=null){
            slowPoint=slowPoint.next;
        }
        return slowPoint;
    }
}
